<?php

class EvaluationsController extends AppController
{

    var $layout = 'evaluator_default';
    var $uses = array('Evaluation', 'Congress', 'Article', 'User');

    function evaluator_list($congressId)
    {
        $userId = $this->Session->read('Auth.User.id');
        $articles = $this->paginate('Evaluation', array(
                    'Article.congress_id' => $congressId,
                    'Evaluation.user_id' => $userId));

        $this->set('articles', $articles);
        $congress = $this->Congress->findById($congressId);
        $this->set('congress', $congress);
    }

    function evaluator_evaluate($congressId, $articleId)
    {
        $userId = $this->Session->read('Auth.User.id');
        $verify = $this->Evaluation->find('first', array('conditions' => array(
                        'Evaluation.article_id' => $articleId,
                        'Evaluation.user_id' => $userId
                        )));

        if ($verify) {
            if (empty($this->data)) {
                $this->data = $verify;
            } else {
                $this->data['Evaluation']['note'] = "'" . $this->data['Evaluation']['note'] . "'";
                // Calcula a média da nota para salvar em 'mean_grade'
                $congress = $this->Congress->findById($congressId);
                $media =
                        $this->data['Evaluation']['originality_grade'] * $congress['Congress']['originality_value'] / 100 +
                        $this->data['Evaluation']['relevance_grade'] * $congress['Congress']['relevance_value'] / 100 +
                        $this->data['Evaluation']['text_grade'] * $congress['Congress']['text_value'] / 100 +
                        $this->data['Evaluation']['general_grade'] * $congress['Congress']['general_value'] / 100;

                $this->data['Evaluation']['mean_grade'] = number_format($media, 2, ".", ",");
                if ($this->Evaluation->updateAll($this->data['Evaluation'], array(
                            'Evaluation.article_id' => $articleId,
                            'Evaluation.user_id' => $userId
                        ))) {
                    $this->Evaluation->setStatus($articleId, $userId, 'AP');
                    $this->Session->setFlash(__('Sua avaliação foi salva', true));
                    $this->redirect(array('evaluator' => true, 'controller' => 'evaluations', 'action' => 'list', $congressId));
                } else {
                    $this->Session->setFlash(__('Erro ao salvar o Avaliação', true));
                }
            }
            $this->set('evaluation', $verify);
            $this->set('congressId', $congressId);
            $this->set('articleId', $articleId);
        } else {
            $this->Session->setFlash('Você não tem autorização para avaliar este artigo', true);
            $this->redirect('/');
        }
    }

    function evaluator_endevaluation($congressId, $articleId)
    {
        $userId = $this->Session->read('Auth.User.id');
        $verify = $this->Evaluation->find('first', array('conditions' => array(
                        'Evaluation.article_id' => $articleId,
                        'Evaluation.user_id' => $userId
                        )));
        if ($verify) {
            $this->Evaluation->setStatus($articleId, $userId, 'AV');
            $this->redirect(array('evaluator' => true, 'controller' => 'evaluations', 'action' => 'list', $congressId));
        } else {
            $this->Session->setFlash('Você não tem autorização para avaliar este artigo', true);
            $this->redirect('/');
        }
    }

    function coordinator_distributeArticles($congressId, $evaluationsNumber)
    {
        $articles = $this->Article->find('all', array('conditions' => array(
                        'Article.congress_id' => $congressId
                        )));

        $evaluators = $this->User->find('all', array('conditions' => array(
                        'User.group_id' => 3
                        )));
        $this->Evaluation->distributeArticles($evaluationsNumber, $articles);
        $this->Session->setFlash(__('O período de Submissão foi Encerrado.', true));
        $this->redirect(array('controller' => 'congresses', 'action' => 'index'));
    }

}

?>